package com.flying.cms.api;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.util.Codes;
import com.flying.common.util.StringUtils;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.annotation.event.MethodListener;
import com.flying.framework.annotation.event.MethodListenerHandler;
import com.flying.framework.data.Data;
import com.flying.framework.service.AbstractService;

@Service(value="api.content")
public class Content  extends AbstractService{

	@MethodInfo("根据栏目ID查询")
	@DaoQuery(entity="v_content_simple", wsql="del_flag=0 and status=1", osql="order by sort_id desc", pageable=true)
	public Data findByCategoryId(

			@Param(value="category_id", required=true, desc="栏目ID") int category_id,
			@Param(value="tag", required=false, desc="标签", tag="like") String tag,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows
		
		) throws Exception {
		return null;
	}

	@MethodInfo("根据栏目代码查询")
	public Data findByCategoryCode(

			@Param(value="code", required=false, desc="栏目代码") String code,
			@Param(value="tag", required=false, desc="标签") String tag,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows
		
		) throws Exception {
		
		Category cs = module.getService(Category.class);
		final Data c = cs.findByCode(code);
		final int category_id = c.getInteger("category_id", 0);
		Data res = this.findByCategoryId(category_id, (tag == null?"":tag.trim())+"%", page, rows);
		//
		List<Map<String, Object>> list = res.get(Codes.ROWS);
		for(Map<String, Object> r: list) {
			this.resetIconFields(r);
		}
		//
		return res;
	}

	@MethodInfo("根据内容主键ID查询")
	@MethodListener(trigger="findByContentId",	handlers={@MethodListenerHandler(serviceId="ContentService:reads")})
	public Data findByContentId(
		
			@Param(value="content_id", required=true, desc="") int content_id
		
		) throws Exception {
		
		Data res = this.daoFindByContentId(content_id);
		this.resetIconFields(res.getValues());
		return res;
	}

	@DaoQuery(entity="t_content", single=true, wsql="del_flag=0 and status=1")
	protected Data daoFindByContentId(
		
			@Param(value="content_id", required=true, desc="") int content_id
		
		) throws Exception {
		return null;
	}
	
	private void resetIconFields(Map<String, Object> values) {
		final String uploadURL = module.getModuleConfig().getConfig("uploadURL");
		if(StringUtils.isEmpty(uploadURL)) return ;
		
		String icon = (String)values.get("icon");
		if(!StringUtils.isEmpty(icon))
			values.put("icon", uploadURL+icon);
		
		String icon2 = (String)values.get("icon2");
		if(!StringUtils.isEmpty(icon2))
			values.put("icon2", uploadURL+icon2);
		
		String icon3 = (String)values.get("icon3");
		if(!StringUtils.isEmpty(icon3))
			values.put("icon3", uploadURL+icon3);
		
		String icon4 = (String)values.get("icon4");
		if(!StringUtils.isEmpty(icon4))
			values.put("icon4", uploadURL+icon4);
	}
	//----------------------------------------------------------------------------
	@MethodInfo("创建新对象")
	@DaoCreate(entity="t_content")
	public Data create(
		
			@Param(value="category_id", required=false, desc="栏目ID") int category_id,
			@Param(value="title", required=false, desc="标题") String title,
			@Param(value="summary", required=false, desc="摘要") String summary,
			@Param(value="content", required=false, desc="内容") String content,
			@Param(value="icon", required=false, desc="图片1") String icon,
			@Param(value="icon2", required=false, desc="图片2") String icon2,
			@Param(value="icon3", required=false, desc="图片3") String icon3,
			@Param(value="icon4", required=false, desc="图片4") String icon4,
			@Param(value="link", required=false, desc="链接") String link,
			@Param(value="source", required=false, desc="来源") String source,
			@Param(value="tag", required=false, desc="标签") String tag,
			@Param(value="tag2", required=false, desc="标签2") String tag2,
			@Param(value="sort_id", required=false, desc="排序") int sort_id,
			@Param(value="status", required=false, desc="状态（0：未发布；1：已发布）") String status,
			@Param(value="browsers", required=false, desc="浏览次数") int browsers,
			@Param(value="f0", required=false, desc="扩展0") String f0,
			@Param(value="f1", required=false, desc="扩展1") String f1,
			@Param(value="f2", required=false, desc="扩展2") String f2,
			@Param(value="f3", required=false, desc="扩展3") String f3,
			@Param(value="f4", required=false, desc="扩展4") String f4,
			@Param(value="f5", required=false, desc="扩展5") String f5,
			@Param(value="f6", required=false, desc="扩展6") String f6,
			@Param(value="f7", required=false, desc="扩展7") String f7,
			@Param(value="f8", required=false, desc="扩展8") String f8,
			@Param(value="f9", required=false, desc="扩展9") String f9,
			@Param(value="del_flag", required=false, desc="是否删除（0：未删除；1：已删除") int del_flag,
			@Param(value="creator", required=false, desc="创建人") String creator,
			@Param(value="updator", required=false, desc="修改人") String updator,
			@Param(value="publisher", required=false, desc="发布人") String publisher,
			@Param(value="publish_date", required=false, desc="发布时间") Date publish_date,
			@Param(value="create_date", required=false, desc="创建时间") Date create_date,
			@Param(value="update_date", required=false, desc="修改时间") Date update_date
		
		) throws Exception {
		
		return new Data(
				"status", 0,
				"del_flag", 0,
				"create_date", new Date(),
				"update_date", new Date()
			);
	}

	@MethodInfo("修改对象")
	@DaoUpdate(entity="t_content")
	public Data update(
		
			@Param(value="content_id", required=true, desc="内容ID") int content_id,
			@Param(value="category_id", required=false, desc="栏目ID") int category_id,
			@Param(value="title", required=false, desc="标题") String title,
			@Param(value="summary", required=false, desc="摘要") String summary,
			@Param(value="content", required=false, desc="内容") String content,
			@Param(value="icon", required=false, desc="图片1") String icon,
			@Param(value="icon2", required=false, desc="图片2") String icon2,
			@Param(value="icon3", required=false, desc="图片3") String icon3,
			@Param(value="icon4", required=false, desc="图片4") String icon4,
			@Param(value="link", required=false, desc="链接") String link,
			@Param(value="source", required=false, desc="来源") String source,
			@Param(value="tag", required=false, desc="标签") String tag,
			@Param(value="tag2", required=false, desc="标签2") String tag2,
			@Param(value="sort_id", required=false, desc="排序") int sort_id,
			@Param(value="status", required=false, desc="状态（0：未发布；1：已发布）") String status,
			@Param(value="browsers", required=false, desc="浏览次数") int browsers,
			@Param(value="f0", required=false, desc="扩展0") String f0,
			@Param(value="f1", required=false, desc="扩展1") String f1,
			@Param(value="f2", required=false, desc="扩展2") String f2,
			@Param(value="f3", required=false, desc="扩展3") String f3,
			@Param(value="f4", required=false, desc="扩展4") String f4,
			@Param(value="f5", required=false, desc="扩展5") String f5,
			@Param(value="f6", required=false, desc="扩展6") String f6,
			@Param(value="f7", required=false, desc="扩展7") String f7,
			@Param(value="f8", required=false, desc="扩展8") String f8,
			@Param(value="f9", required=false, desc="扩展9") String f9,
			@Param(value="del_flag", required=false, desc="是否删除（0：未删除；1：已删除") int del_flag,
			@Param(value="creator", required=false, desc="创建人") String creator,
			@Param(value="updator", required=false, desc="修改人") String updator,
			@Param(value="publisher", required=false, desc="发布人") String publisher,
			@Param(value="publish_date", required=false, desc="发布时间") Date publish_date,
			@Param(value="create_date", required=false, desc="创建时间") Date create_date,
			@Param(value="update_date", required=false, desc="修改时间") Date update_date
		
		) throws Exception {
		
		return null;
	}

	@MethodInfo("删除对象")
	@DaoRemove(entity="t_content")
	public Data remove(
		
			@Param(value="content_id", required=true, desc="") int content_id
		
		) throws Exception {
		
		return null;
	}

	@MethodInfo("查询所有对象")
	@DaoQuery(entity="t_content")
	public Data findAll() throws Exception {
		return null;
	}

}